// pages/home/topicDetail/index.js
import {
  BASE_URL
} from '../../../constant/index'
import {
  getTopicByIdApi,
  getTopiDynamicApi
} from '../../../https/topic'
const app = getApp();
Page({

  /** 
   * 页面的初始数据
   */
  data: {
    BASE_URL,
    // 顶部标题栏动态设置
    topBarInfo: {
      navBarHeight: app.globalData.navBarHeight,
      menuRight: app.globalData.menuRight,
      menuTop: app.globalData.menuTop,
      menuHeight: app.globalData.menuHeight,
    },
    detail: {},
    list: [],
    total: 0,
    order: 'hit'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    this.getTopicDetail(options.docid)
    this.getTopiDynamic(options.docid)
  },
  // 获取话题圈详情
  async getTopicDetail(id) {
    const getRes = await getTopicByIdApi({
      id
    })
    if (getRes.code == 200) {
      this.setData({
        detail: getRes.data
      })
    }
  },

  // 获取话题圈动态
  async getTopiDynamic(id) {
    const order = this.data.order
    console.log(order)
    const getRes = await getTopiDynamicApi({
      id,
      order
    })
    console.log(getRes)
    if (getRes.code == 200) {
      this.setData({
        list: getRes.data.list,
        total: getRes.data.total
      })
    }
  },
  // 切换获取动态
  async toggleOrder(e) {
    console.log(e)
    const order = e.currentTarget.dataset.order
    console.log(order)
    this.setData({
      order
    })
    wx.showLoading({})
    await this.getTopiDynamic(this.data.detail.id)
    wx.hideLoading()
  },
  gotoDetail(e) {
    console.log(e)
    const docid = e.currentTarget.dataset.docid

    wx.navigateTo({
      url: '../../home/detail/index?docid=' + docid,
    })
  },
  back() {
    wx.navigateBack()
  }
})